//
//  DDProductIntroCell.m
//  DingDongZP
//
//  Created by weily on 16/5/26.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDProductIntroCell.h"

@implementation DDProductIntroCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.nameLab = [[UILabel alloc]init];
        self.nameLab.textColor = COLOR_FONT_MAIN_GREEN;
        self.nameLab.text = @"产品介绍";
        self.nameLab.font = [UIFont systemFontOfSize:17];
        [self.contentView addSubview:self.nameLab];
        
        WEAKSELF
        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf);
            make.height.equalTo(@(30));
            make.centerX.equalTo(weakSelf);
        }];
        
        UILabel *leftView = [[UILabel alloc]init];
        leftView.textColor = COLOR_FONT_LIGHT_GRAY;
        leftView.text = @"----------------";
        leftView.textAlignment = 0;
        leftView.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:leftView];
        [leftView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf).offset(13);
            make.left.equalTo(@(10));
            make.height.equalTo(@(3));
            make.right.equalTo(self.nameLab.mas_left);
        }];
        
        UILabel *rightView = [[UILabel alloc]init];
        rightView.textColor = COLOR_FONT_LIGHT_GRAY;
        rightView.text = @"----------------";
        rightView.textAlignment = 2;
        rightView.adjustsFontSizeToFitWidth = YES;
        [self.contentView addSubview:rightView];
        [rightView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf).offset(13);
            make.left.equalTo(self.nameLab.mas_right);
            make.height.equalTo(@(3));
            make.right.equalTo(weakSelf).offset(-10);
        }];
        
        
        self.iconView = [[UIImageView alloc]init];
        [self.contentView addSubview:self.iconView];
        [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(70, 70));
            make.top.equalTo(self.nameLab.mas_bottom).offset(20);
            make.centerX.equalTo(weakSelf);

        }];
        self.iconView.layer.cornerRadius = 35;
        self.iconView.layer.masksToBounds = YES;

        
        self.productIntroduceTitle = [[UILabel alloc]init];
        self.productIntroduceTitle.textColor = COLOR_FONT_BLACK;
        self.productIntroduceTitle.font = [UIFont systemFontOfSize:15];
        self.productIntroduceTitle.textAlignment = 1;
        [self.contentView addSubview:self.productIntroduceTitle];
        [self.productIntroduceTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.iconView.mas_bottom).offset(20);
            make.left.equalTo(@(0));
            make.right.equalTo(weakSelf);
            make.height.equalTo(@(30));
        }];

        self.infoLab = [[UILabel alloc]init];
        self.infoLab.textColor = COLOR_FONT_BLACK;
        self.infoLab.font = [UIFont systemFontOfSize:15];
        self.infoLab.numberOfLines = 0;
        [self.contentView addSubview:self.infoLab];
        [self.infoLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.productIntroduceTitle.mas_bottom).offset(20);
            make.left.equalTo(@(50));
            make.right.equalTo(weakSelf).offset(-50);
            make.height.equalTo(@(20));
        }];
    }
    
    return self;
}

+(instancetype)loadTableView:(UITableView *)tableView
{
    NSString *simpleIdentify = @"DDProductIntroCell";
    DDProductIntroCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleIdentify];
    if(cell == nil)
    {
        cell = [[DDProductIntroCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleIdentify];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

- (void)setDetail:(DDCompanyInfo *)detail {
    
    _detail = detail;
 
    //  产品介绍
    NSString *urlString = [DDAPIGenerate imageURLWithPath:detail.product_logo];
    
    [self.iconView  sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"default_header"]];
    
    if (!detail.product_name) {
        detail.product_name = @"未填写";
    }
    if (!detail.product_strengths) {
        detail.product_strengths = @"未填写";
    }
    self.productIntroduceTitle.text = [NSString stringWithFormat:@"%@ | %@",detail.product_name,detail.product_strengths];
    
    if (!detail.product_highlights) {
        detail.product_highlights = @"未填写";
    }
    self.infoLab.text = detail.product_highlights;

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    CGSize size = [self.infoLab sizeThatFits:CGSizeMake(_infoLab.width, 1000)];
    _infoLab.height = size.height;
    self.height = CGRectGetMaxY(self.infoLab.frame)+20;
    self.infoLab.height = [[self class]textHeight:self.infoLab.text] ;
}

+ (CGFloat)textHeight:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width / 3 * 2, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:17]} context:nil];
    
    return rect.size.height;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end

